Class Nur.MNISV3.Service.NurEmr Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// Creator:      lmm
/// CreatDate:    2019-05-29     
///   w ##class(Nur.MNISV3.Service.NurEmr).GetNewEmrList("2^2025-04-15^00:00^2025-04-22^23:59^DHCNURSHJLWHLJLDLR905^false")
ClassMethod GetNewEmrList(parr) As %String
{
    n (parr)
    s ^MML("getNewEmrList")=parr
    s $zt="errGetNewEmrList"
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    ;b //1
    i parr["&" s parr=$tr(parr,"&",":")
    s Adm=$P(parr,"^",1)
    s StTime=$P(parr,"^",3)
    i StTime="" s StTime="0:00"
    s EndTime=$P(parr,"^",5)
    i EndTime="" s EndTime="0:00"
    s stdate=$P(parr,"^",2)
    s stdate=##class(websys.Conversions).DateHtmlToLogical(stdate)
    s eddate=$P(parr,"^",4)
    s eddate=##class(websys.Conversions).DateHtmlToLogical(eddate)
    s StTime=$ZTH(StTime)
    s EndTime=$ZTH(EndTime)
    
    s RecTyp=$P(parr,"^",6)
    s RecTyp=$ZConvert(RecTyp,"U")
    s TCIdent=##class(NurMp.Template).GetTCIndentity(RecTyp)
    s TCIdent=$ZConvert(TCIdent,"U")
    s IfCancelRec=$P(parr,"^",7)
    s nurseSign=$P(parr,"^",8)
    s currHeadDr=$P(parr,"^",9)
    s par=$O(^Nur.DHCNurseRecParentI("Episode"," "_Adm,""))
    //分项统计对应列
    s ret=##class(Nur.DHCNurseRecSet).getSet(RecTyp)
    s PartInProperty=$P(ret,"^",3)
    s PartInNameProperty=$P(PartInProperty,"&",1)
    s PartInAmountProperty=$P(PartInProperty,"&",2)
    s PartOutProperty=$P(ret,"^",4)
    s PartOutNameProperty=$P(PartOutProperty,"&",1)
    s PartOutAmountProperty=$P(PartOutProperty,"&",2)
    s AdmDate=$p($g(^PAADM(Adm)),"^",6)
    s curDate=+$h

    s dataList=##class(%ListOfDataTypes).%New() //护理记录数据列表
    s titleStr=##class(Nur.MNISV3.Service.Config).GetTemplateTitle(Adm,RecTyp) // EH title加入配置
    s titleLen=$l(titleStr,"^")
    s noteCode=$p($p(titleStr,"^",3),"|",2)
    i (noteCode="") s noteCode="CareDesc"
    f date=curDate:-1:AdmDate
    {
        s tim="" f  s tim=$O(^NurMp.DHCTempMultDataI("TypAdmDatTim"," "_TCIdent," "_Adm," "_$zd(date,3),tim),-1) q:tim=""  d
        .s PGDID="" f  s PGDID=$O(^NurMp.DHCTempMultDataI("TypAdmDatTim"," "_TCIdent," "_Adm," "_$zd(date,3),tim,PGDID),-1) q:PGDID=""  d
        ..s PGD=##class(NurMp.DHCTempMultData).%OpenId(PGDID)
        ..;b //2
        ..q:(tim=" ") ;时间值为空过滤
        ..q:(PGD.RecCancelDate'="")
        ..w PGD.HeadDR,!
        ..q:(currHeadDr'="")&&(PGD.HeadDR'=currHeadDr)
        ..q:(currHeadDr="")&&(PGD.HeadDR'="")
        ..s dataSub=##class(%ArrayOfDataTypes).%New()  //每条数据内容
        ..d dataSub.SetAt($zd(date,3),"CareDate")
        ..d dataSub.SetAt($E(tim,1,6),"CareTime")
        ..d dataSub.SetAt($P($g(^SSU("SSUSR",+PGD.RecUser)),"^",2),"CareUser")
        ..d dataSub.SetAt("","CareDesc") 
        ..;s modelId=$o(^CT.NUR.MNIS.EmrModelI("codeIndex"," "_RecTyp,""))
        ..;i modelId'="" s noteCode=$listget(^CT.NUR.MNIS.EmrModelD(modelId),9)
        ..;e  s noteCode=""
        ..i noteCode'="" d
        ...s careDesc=..ZObjProperty(PGD,noteCode)
        ...d dataSub.SetAt($g(careDesc),"CareDesc")
        ..;i RecTyp="DHCNURBARTHELLR" d dataSub.SetAt(PGD.Item11,"CareDesc")
        ..;i RecTyp="DHCNURDDFXPGJHLJLDLR" d dataSub.SetAt(PGD.Item46,"CareDesc")
        ..;i RecTyp="DHCNURYCFXPGJHLJLDCRHZLR" d dataSub.SetAt(PGD.Item9,"CareDesc")    
        ..d dataSub.SetAt(PGDID,"par")
        ..d dataSub.SetAt("","rw")
        ..d dataList.Insert(dataSub)
        ..f titleInd=1:1:titleLen d // EH
        ...s item=$p($p(titleStr,"^",titleInd),"|",2)
        ...q:(item="")||(dataSub.IsDefined(item))
        ...s value=..ZObjProperty(PGD,item)
        ...s value=..NumberValue(value)
        ...d dataSub.SetAt(value,item)
     }
    
    //设置表头   
    s titleList=##class(%ListOfDataTypes).%New()
    //s titleStr="日期|CareDate^时间|CareTime^分值|CareDesc^记录人|CareUser"
    f i=1:1:$l(titleStr,"^")
    {
        s titleDataStr=$p(titleStr,"^",i)
        continue:titleDataStr=""
        s titleDesc=$p(titleDataStr,"|",1)
        s titleCode=$p(titleDataStr,"|",2)
        s titleSub=##class(%ArrayOfDataTypes).%New()
        d titleSub.SetAt(titleDesc,"TitleDesc")
        d titleSub.SetAt(titleCode,"TitleCode")
        d titleList.Insert(titleSub)
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(titleList),"titleList")
    d json.SetAt($g(dataList),"dataList")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)

errGetNewEmrList
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// Creator：      JYW 
/// CreatDate：    2018-11-26 
/// Description:   模板解析输出
/// Input：        locId 科室Id，EmrCode 模板关键字，funname js方法名
/// Output：       
/// Others： w ##class(Nur.MNISV3.Service.NurEmr).saveNewEmrData("30ed6ca8f3e8427492ddd90df14464a1","3242491","","{""TextElement_361"":""内分泌代谢病科"",""TextElement_363"":""08"",""TextElement_365"":""张万顺"",""TextElement_367"":""1196373"",""TextElement_369"":""男"",""TextElement_371"":""41岁"",,""DateElement_374"":""2020-06-29"",""TimeElement_375"":""11:26"",""TextElement_377"":""2型糖尿病,睡眠障碍"",""DateElement_379"":"""",""TimeElement_380"":"""",""TextElement_382"":"""",""RadioElement_384"":[{""NumberValue"":""2"",""Text"":""门诊"",""Value"":""2""}],""RadioElement_388"":[{""NumberValue"":""2"",""Text"":""扶行"",""Value"":""2""}],""RadioElement_394"":[{""NumberValue"":""2"",""Text"":""城乡居民医保"",""Value"":""2""}],""TextElement_398"":"""",,""TextElement_402"":""36.1"",""TextElement_405"":""86"",""TextElement_408"":"""",""TextElement_411"":""18"",""TextElement_414"":""132"",""TextElement_416"":""80"",""RadioElement_419"":[],""TextElement_424"":"""",""CheckElement_585"":[{""NumberValue"":""2"",""Text"":""苍白"",""Value"":""2""}],""TextElement_433"":"""",""RadioElement_435"":[{""NumberValue"":""1"",""Text"":""完整:"",""Value"":""1""}],""TextElement_437"":"""",,""TextElement_440"":"""",""TextElement_442"":"""",""CheckElement_592"":[{""NumberValue"":""1"",""Text"":""正常"",""Value"":""1""}],""RadioElement_599"":[],""TextElement_451"":"""",""RadioElement_454"":[{""NumberValue"":""4"",""Text"":""保留尿管"",""Value"":""4""}],""TextElement_462"":"""",""RadioElement_464"":[],""TextElement_473"":"""",""RadioElement_475"":[],""TextElement_478"":"""",""RadioElement_480"":[],""TextElement_485"":"""",""RadioElement_487"":[],,""RadioElement_493"":[],""TextElement_495"":"""",,""TextElement_497"":"""",,""RadioElement_501"":[],""TextElement_506"":"""",""CheckElement_606"":[],""TextElement_513"":"""",""RadioElement_515"":[],""TextElement_518"":"""",""RadioElement_520"":[],""RadioElement_526"":[],""TextElement_528"":"""",""RadioElement_530"":[],""TextElement_533"":"""",""TextElement_535"":"""",""TextElement_537"":"""",""RadioElement_539"":[],,""RadioElement_546"":[],""RadioElement_547"":[],,""TextElement_550"":"""",,""RadioElement_623"":[],""TextElement_557"":"""",,""RadioElement_613"":[],""TextElement_565"":"""",,""RadioElement_619"":[],""RadioElement_571"":[],""TextElement_573"":"""",""RadioElement_575"":[],""TextElement_578"":"""",""TextElement_580"":""CA刘皓*4074"",""DateElement_582"":""2020-07-14"",""TimeElement_583"":""19:36:49"",""EpisodeID"":""3242491"",""LOGON.CTLOCID"":""691"",""LOGON.GROUPDESC"":""住院护士"",""LOGON.USERID"":""4074"",""LOGON.WARDID"":""73"",""NurMPDataID"":"""",""TemporarySave"":""1"",""templateVersionGuid"":""30ed6ca8f3e8427492ddd90df14464a1""}","住院护士","691","73","4074")   
ClassMethod SaveNewEmrData(guId, episodeId, recordId, parr, groupDesc, locId, wardId, userId)
{
    n (guId, episodeId,recordId,parr,groupDesc,locId,wardId,userId,%request,%session)
    s ^MML("saveNewEmrData",guId_"-")=$lb(guId, episodeId,recordId,parr,groupDesc,locId,wardId,userId)
    s $zt="errSaveNewEmrData"
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s parrObj=##class(Nur.MNISV3.Common.JSON).Decode(parr)
    
    //SessionVal 这参数简直了，护理组太煞笔
    //s CTLOCID=SessionVal.GetAt("LOGON.CTLOCID")
    //s WARDID=SessionVal.GetAt("LOGON.WARDID")
    //s GROUPDESC=SessionVal.GetAt("LOGON.GROUPDESC")
    //s USERID=SessionVal.GetAt("LOGON.USERID")
    s EmrCode=##class(NurMp.Template).GetIndentity(guId)
    s ListCode=##class(Nur.MNISV3.Service.Config).GetTemplateListCode(episodeId,EmrCode)
    s SessionVal=##class(%ArrayOfDataTypes).%New()
    i ($d(%session)) d
    .d SessionVal.SetAt(%session.Get("LOGON.CTLOCID"),"LOGON.CTLOCID")
    .d SessionVal.SetAt(%session.Get("LOGON.WARDID"),"LOGON.WARDID")
    .d SessionVal.SetAt(%session.Get("LOGON.GROUPDESC"),"LOGON.GROUPDESC")
    .d SessionVal.SetAt(%session.Get("LOGON.USERID"),"LOGON.USERID")
    .d SessionVal.SetAt(ListCode,"SubjectionTemplateGuid")
    s SessionVal="^"_##class(Nur.MNISV3.Common.JSON).Encode(SessionVal)
    //##class(NurMp.DHCTempMultData).save(TCIndentity, EpisodeId, RecordId, ParaArray, GroupDesc, Loc, Ward, UserId, TemporarySave, printTemplateEmrCode, imgElementIdMarkFree)
    s ret=##class(NurMp.DHCTempMultData).save(guId,episodeId,recordId,parrObj,groupDesc,locId,wardId,userId,1,SessionVal,"")
    //q ret ##class(NurMp.ErrorMsg).getJsonInfo(returnText,msg,status,info)
    s retObj=##class(Nur.MNISV3.Common.JSON).Decode(ret)
    s retData=retObj.GetAt("data") //returnText
    s retStatus=retObj.GetAt("status")
    s retMsg=retObj.GetAt("msg")
    //CA保存护理病历--有文字提示
    i +retData=0 s msg=retData,Status="-1",msgcode="100000"
    ;i retStatus="-1" s msg=retMsg,Status="-1",msgcode="100001"
    i retStatus'=0 s msg=retMsg,Status="-1",msgcode="100001"
    s caFlag=##class(Nur.MNISV3.Service.CASign).GetCAServiceStatus(locId,userId)
    
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(caFlag),"caFlag")
    d json.SetAt($g(retData),"RetData") //返回recId
    
    
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    
errSaveNewEmrData
    s msg=$ze
    B
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// Creator：      JYW 
/// CreatDate：    2018-11-29 
/// Description:   获取PDA显示模板列表
/// Input：        locId 科室Id，episodeId 就诊号
/// Output：       
/// Others： w ##class(Nur.MNISV3.Service.NurEmr).GetModelList("8","1091","20665","") 
ClassMethod GetModelList(locId, episodeId, userId = "", tab = "", hospitalId = "")
{
    n (locId,episodeId,userId,tab,hospitalId,%session)
    s $zt="errGetModelList"
    s ^EH("GetModelList")=$lb(locId,episodeId,userId,tab,hospitalId)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s menuList=##class(%ListOfDataTypes).%New()
    ;s ^MML("getModelList")=$lb(locId,episodeId,userId)
    /*s hospitalId=$p(^CTLOC(locId),"^",22)
    i '$D(^CF.NUR.MNIS.DepModelSetI("locIndex"," "_locId)) s locId="common"_hospitalId
    s locId=$ZConvert(locId,"U")
    s id=""
    f  s id=$o(^CF.NUR.MNIS.DepModelSetI("locIndex"," "_locId,id)) q:id=""  d
    .s setDta=^CF.NUR.MNIS.DepModelSetD(id)
    .s menuId=$listget(setDta,4)
    .q:menuId=""
    .q:'$d(^CF.NUR.MNIS.EmrMenuD(menuId))
    .s menuData=^CF.NUR.MNIS.EmrMenuD(menuId)
    .s setID(menuId,id)=1*/
    s exist=##class(%Dictionary.MethodDefinition).%ExistsId("Nur.HISUI.MNIS.Config||GetTemplatesLoc")
    i exist d GetModelTabs ;如果不需要科室模板功能注释此行
    d GetModelList
    s sort="",preMenuName=""
    zw setID
    f  s sort=$o(setID(sort)) q:sort=""  d
    .s menuData=^CF.NUR.MNIS.EmrMenuD(sort)
    .s menuName=$listget(menuData,2)
    .s menuCode=$listget(menuData,3)
    .s modelArr=##class(%ArrayOfDataTypes).%New()
    .s ModelList=##class(%ListOfDataTypes).%New()
    .s setId=""
    .f  s setId=$o(setID(sort,setId)) q:setId=""  d
    ..s setDta=$g(^CF.NUR.MNIS.DepModelSetD(setId))
    ..s modelCode=$listget(setDta,3)
    ..s modelName=$listget(setDta,8)
    ..i (modelName="") d
    ...s modelID=$o(^CF.NUR.MNIS.EmrModelI("codeIndex"," "_$zcvt(modelCode,"U"),""))
    ...i (modelID'="") d
    ....s modelObject=##class(CF.NUR.MNIS.EmrModel).%OpenId(modelID)
    ....s modelName=modelObject.ModelName
    ....d modelObject.%Close()
    ..s modelType=$listget(setDta,9)
    ..s linkModel=$listget(setDta,10)
    ..s modelNum=$listget(setDta,11)
    ..s getValMth=$listget(setDta,12)
    ..s getListMth=$listget(setDta,13)
    ..s saveMth=$listget(setDta,14)
    ..s ModeInfo=##class(%ArrayOfDataTypes).%New()
    ..d ModeInfo.SetAt(modelCode,"modelCode")
    ..d ModeInfo.SetAt(modelName,"modelName")
    ..d ModeInfo.SetAt(##class(NurMp.Template).GetTCIndentity(modelCode),"guId")
    ..d ModeInfo.SetAt("","linkModel")
    ..d ModeInfo.SetAt(3,"modelType")
    ..s tcId=""
    ..s guId=##class(NurMp.Template).GetTCIndentity(modelCode)
    ..s TCIndent=$ZConvert(guId,"U")
    ..s tcId=$o(^NurMp.TemplateI("TCIndentity"," "_TCIndent,tcId))
    ..i tcId'=""  d
    ...s tempalte=##class(NurMp.Template).%OpenId(tcId)
    ...s SaveType=tempalte.SaveType
    ...i SaveType'=2  d ModeInfo.SetAt(1,"modelType")
    ...d tempalte.%Close()
    ..d ModeInfo.SetAt("","linkModel")
    ..d ModeInfo.SetAt("","modelNum")
    ..d ModeInfo.SetAt("","getValMth")
    ..d ModeInfo.SetAt("","getListMth")
    ..d ModeInfo.SetAt("","saveMth")
    ..;s wFlag=##class(web.DHCNUREMRNEWOnPage).GetModelDataStatus(episodeId,modelCode)
    ..;i wFlag'=0 s wFlag=1
    ..s wFlag=1
    ..i ("A"[$g(tab)) s wFlag=..IsFilled(episodeId,modelCode)
    ..q:("A"=$g(tab))&&(wFlag'=1)
    ..d ModeInfo.SetAt(wFlag,"wStatus")
    ..w episodeId_"|"_TCIndent_"|"_locId_"|"_hospitalId,!
    ..s exist=##class(%Dictionary.MethodDefinition).%ExistsId("NurMp.Template.PDASaveInterface||getHeadInterface")
    ..i exist s DTTObj=..GetDTTObj(episodeId,TCIndent,locId,hospitalId) 
    ..e  s DTTObj= ##class(Nur.MNISV3.Service.NurEmrConfig).GetChangeHeadObj(modelCode,episodeId,userId)
    ..d ModeInfo.SetAt(DTTObj,"DTTObj")
    ..d ModeInfo.SetAt("0","tableFlag") //所有都用新版,默认0 , 1支持表格-根据DocumentSetting的<Width>1000</Width>
    ..i modelCode="DHCNURSHZLNLADLPGDLR" d ModeInfo.SetAt("1","tableFlag")
    ..i modelCode="DHCNURHLJLDCHLRNS" d ModeInfo.SetAt("","tableFlag")
    ..i modelCode="DHCNURTTLR" d ModeInfo.SetAt("0","tableFlag")
    ..d ModelList.Insert(ModeInfo)
    .q:(ModelList.Count()=0)
    .d modelArr.SetAt(menuCode,"menuCode")
    .d modelArr.SetAt(menuName,"menuName")
    .d modelArr.SetAt($G(ModelList),"ModelList")
    .d menuList.Insert(modelArr)
    d json.SetAt(menuList,"menuList")
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.PDA.JSON).Encode(json)

errGetModelList
    s Msg=$ze
    q ##class(Nur.PDA.Common).ReturnMsgJson("获取模板列表错误！"_Msg,"100018","-1")
}

ClassMethod GetDTTObj(episodeId, TCIndent, locId, hospitalId)
{
        // "DTTObj": {
    //                     "source": [
    //                         {
    //                             "Text": "2024-11-29(16:35) Y 1-血糖mmol/L 2-CVPcmH2O 3- 4- 5- 6-",
    //                             "Value": "206"
    //                         },
    //                         {
    //                             "Text": "2024-11-29(15:55) N 1-意识 2- 3- 4- 5- 6-",
    //                             "Value": "205"
    //                         }
    //                     ],
    //                     "values": [
    //                         {
    //                             "Text": "2024-11-29(16:35) Y 1-血糖mmol/L 2-CVPcmH2O 3- 4- 5- 6-",
    //                             "Value": "206"
    //                         }
    //                     ],
    //                     "FormName": "HiddenTextElement_49"
    //                 }
    n (episodeId, TCIndent, locId, hospitalId)
    s ^zlf("GetDTTObj") = $lb(episodeId,TCIndent,locId,hospitalId)
    s head = ##class(NurMp.Template.PDASaveInterface).getHeadInterface(episodeId,TCIndent,locId,hospitalId)
    s res = ##class(%ArrayOfDataTypes).%New()
    s default =  head.GetAt("values").GetAt(1).GetAt("Text")
    q:default["空表头" res
    s source = head.GetAt("source")
    s SourceValueKey=source.Next("")
    s ItmValue=##class(%ListOfDataTypes).%New()
    while (SourceValueKey'="") 
    {
        s Value=source.GetAt(SourceValueKey)
        s text = Value.GetAt("Text")
        s text = $replace(text," Y",Value.GetAt("Value"))
        s text = $replace(text," N",Value.GetAt("Value"))
        d Value.SetAt(text,"SelectValue")
        d ItmValue.Insert(Value)
        s SourceValueKey=source.Next(SourceValueKey)
    }
    d res.SetAt(ItmValue,"DTTItems")
    d res.SetAt(head.GetAt("formName"),"FormName")
    q res
}

ClassMethod AddModelList(emrCode, locId, tab) As %String
{
    n (emrCode,locId,tab,%session)
    ;s ^EH("AddModelList")=$lb(emrCode,locId,tab)
    s $zt="errGetModelList"
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s tabBak=tab,tab="L"
    d GetModelList
    s tab=tabBak
    s sort="",findId=""
    f  s sort=$o(setID(sort)) q:sort=""  d
    .s setId=""
    .f  s setId=$o(setID(sort,setId)) q:setId=""  d
    ..s setDta=$g(^CF.NUR.MNIS.DepModelSetD(setId))
    ..s modelCode=$listget(setDta,3)
    ..i (modelCode=emrCode) s findId=setId
    i (tab="L") {
        i (findId'="") {
            d ##class(CF.NUR.MNIS.DepModelSet).%DeleteId(findId)
            s Msg="移出成功"
        } else {
            s Msg="未找到模板"
        }
    } elseif (tab="H") {
        i (findId'="") {
            s Msg="模板已存在"
        } else {
            s mainHospId=##class(Nur.HISUI.MNIS.Config).GetMainHospID(hospId,"N")
            d ##class(Nur.MobileCare.Common).OrgIf(mainHospId,"",.hospOrgId,"I")
            d ##class(Nur.MobileCare.Common).OrgIf(mainHospId,locId,.locOrgId,"I")
            s hospTplId=##class(Nur.HISUI.MNIS.Config).GetTemplateID(emrCode,hospOrgId,"Y")
            i (hospTplId'="") {
                s hospTplObj=##class(CF.NUR.MNIS.DepModelSet).%OpenId(hospTplId)
                s hospFolderId=hospTplObj.MenuId
                s hospFolderObj=##class(CF.NUR.MNIS.EmrMenu).%OpenId(hospFolderId)
                s folderCode=hospFolderObj.MenuCode
                s locFolderId=##class(Nur.HISUI.MNIS.Config).GetFolderID(folderCode,locOrgId)
                i (locFolderId="") {
                    s locFolderObj=##class(CF.NUR.MNIS.EmrMenu).%New()
                    s locFolderObj.LocId=locOrgId
                    s locFolderObj.MenuCode=folderCode
                    s locFolderObj.MenuName=hospFolderObj.MenuName
                    s locFolderObj.CanFlag=hospFolderObj.CanFlag
                    s locFolderObj.MenuSort=hospFolderObj.MenuSort
                    d locFolderObj.%Save()
                    s locFolderId=locFolderObj.%Id()
                    d locFolderObj.%Close()
                }
                s locTplObj=##class(CF.NUR.MNIS.DepModelSet).%New()
                s locTplObj.LocId=locOrgId
                s locTplObj.ModelCode=hospTplObj.ModelCode
                s locTplObj.MenuId=locFolderId
                s locTplObj.ShowName=hospTplObj.ShowName
                s locTplObj.DateItem=hospTplObj.DateItem
                s locTplObj.TimeItem=hospTplObj.TimeItem
                s locTplObj.ValueItem=hospTplObj.ValueItem
                s locTplObj.UserItem=hospTplObj.UserItem
                d locTplObj.%Save()
                d locTplObj.%Close()
                s Msg="加入成功"
            } else {
                s Msg="未找到模板"
            }
        }
    }
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.PDA.JSON).Encode(json)
GetModelList
    s hospId=$p($g(^CTLOC(locId)),"^",22)
    s mainHospId=##class(Nur.HISUI.MNIS.Config).GetMainHospID(hospId,"N")
    i ($g(tab)'="") {
        i (tab="A")||(tab="H") d ##class(Nur.MNISV3.Service.Config).GetTemplateLoc(mainHospId,"","",.setID)
        e  i (tab="L") d ##class(Nur.MNISV3.Service.Config).GetTemplateLoc(mainHospId,locId,"",.setID)
    } else {
        d ##class(Nur.MNISV3.Service.Config).GetTemplate(mainHospId,locId,"",.setID)
    }
    q
GetModelTabs
    i ($g(tab)="") s tab="A"
    s tabItems=##class(%ListOfDataTypes).%New()
    s tabStr="A|已评|1|0^L|科室|0|1^H|全院|0|0"
    s tabLen=$l(tabStr,"^")
    f tabInd=1:1:tabLen {
        s tabItemStr=$p(tabStr,"^",tabInd)
        s tabItem=##class(%ArrayOfDataTypes).%New()
        d tabItem.SetAt($p(tabItemStr,"|",1),"code")
        d tabItem.SetAt($p(tabItemStr,"|",2),"desc")
        d tabItem.SetAt($p(tabItemStr,"|",3),"selected")
        d tabItem.SetAt($p(tabItemStr,"|",4),"refresh")
        d tabItems.Insert(tabItem)
    }
    i ($d(json)) d json.SetAt(tabItems,"tabs")
    q
}

/// Creator:      fengliang
/// CreatDate:    2019-05-29
/// Description:       
///   w ##class(Nur.MNISV3.Service.NurEmr).getFirstId("DHCNURPICCLZHZCYGZS",7)
ClassMethod GetFirstId(RecTyp, Adm) As %String
{
    n (RecTyp, Adm)
    s $zt="errGetFirstId"
    s msg="成功"
    
    s RecTyp=$ZConvert(RecTyp,"U")
    s TCIdent=##class(NurMp.Template).GetTCIndentity(RecTyp)
    s TCIdent=$ZConvert(TCIdent,"U")
    s date="",firstId=""
    s date="" f  s date=$O(^NurMp.DHCTempMultDataI("TypAdmDatTim"," "_TCIdent," "_Adm,date)) q:date=""||(firstId'="")  d
    .s tim="" f  s tim=$O(^NurMp.DHCTempMultDataI("TypAdmDatTim"," "_TCIdent," "_Adm,date,tim)) q:tim=""||(firstId'="")  d
    ..s PGDID="" f  s PGDID=$O(^NurMp.DHCTempMultDataI("TypAdmDatTim"," "_TCIdent," "_Adm,date,tim,PGDID)) q:PGDID=""||(firstId'="")  d
    ...s PGD=##class(NurMp.DHCTempMultData).%OpenId(PGDID)
    ...q:(tim=" ") ;时间值为空过滤
    ...q:(PGD.RecCancelDate'="")
    ...s firstId=PGDID
    q firstId
    
errGetFirstId
    s msg=$ze
    q msg
}

/// Creator：      fengliang
/// CreatDate：    2020-09-04
/// Description:   获取数据源     
/// Others： w ##class(Nur.MNISV3.Service.NurEmr).getDataSource("79||336") 
ClassMethod GetDataSource(dataSourceRef, episodeId) As %String
{
    n (dataSourceRef, episodeId)
    s $zt="errGetDataSource"
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s ret=##class(NurMp.TemplateSet).GetValue(dataSourceRef,episodeId)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt(ret,"DataSource")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    
errGetDataSource
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// Creator:     fengliang
/// CreateDate:  2020-09-07
/// Description: 取所有维护好的知识库科室（改造）
/// Input:       LocId-科室ID
///     w ##class(Nur.MNISV3.Service.NurEmr).getKnowledgeLocs(151)
ClassMethod GetKnowledgeLocs1(LocId As %String) As %String
{
    n (LocId)
    s $zt="errGetKnowledgeLocs1"
    s locs=##class(%ListOfDataTypes).%New()
    //改为只取跟本科室的知识库关联的科室
    s firstId=$o(^NurMp.KnowledgeLocI("Loc"," "_LocId,""),-1)
    q:firstId="" ##class(Nur.JSON).Encode(locs)
    s kcId=$lg(^NurMp.KnowledgeLocD(firstId),5)
    q:kcId="" ##class(Nur.JSON).Encode(locs)
    s id=""
    f {
        s id=$o(^NurMp.KnowledgeLocI("KCIndentity"," "_$zcvt(kcId,"U"),id)) 
        q:id=""
        s locId=$lg(^NurMp.KnowledgeLocD(id),2)
        s locDesc=$p($g(^CTLOC(locId)),"^",2)
        continue:locDesc=""
        s loc=##class(%ArrayOfDataTypes).%New()
        d loc.SetAt(locId,"id")
        d loc.SetAt(locDesc,"text")
        i (locId=LocId) d loc.SetAt("true","selected")
        d locs.Insert(loc)
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt(locs,"locs")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetKnowledgeLocs1
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

ClassMethod GetKnowledgeLocs(locId As %String, hospitalId = "") As %String
{
    n (locId,hospitalId)
    s $zt="errGetKnowledgeLocs"
    ;s ^EH("GetKnowledgeLocs")=$lb(locId,hospitalId)
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s tree=..getKnowledgeLocsPC(hospitalId,locId)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt(tree,"knowledgeLocs")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetKnowledgeLocs
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// Creator:     fengliang
/// CreateDate:  2020-09-07
/// Description: 获取知识库的树结构（改造）
/// Input:       LocId-科室ID
///     w ##class(Nur.MNISV3.Service.NurEmr).GetKnowledgeTree("8","2")
ClassMethod GetKnowledgeTree(locId As %String, hospitalId = "", currLocDr = "") As %String
{
    n (locId,hospitalId,currLocDr)
    s $zt="errGetKnowledgeTree"
    s ^EH("GetKnowledgeTree")=$lb(locId,hospitalId,currLocDr)
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    i ($c(0)'[currLocDr) s locId=currLocDr
    /*s tree=##class(%ListOfDataTypes).%New()
    q:locId="" tree
    s knowLocId=""
    f {
        s knowLocId=$o(^NurMp.KnowledgeLocI("Loc"," "_locId,knowLocId))
        q:knowLocId=""
        s rootIndentity=$lg(^NurMp.KnowledgeLocD(knowLocId),5)
        s rootIndentity=$zcvt(rootIndentity,"U")
        s categoryId=$o(^NurMp.KnowledgeCategoryI("Indentity"," "_rootIndentity,""),-1)
        continue:categoryId=""
        s objCategory=##class(NurMp.KnowledgeCategory).%OpenId(categoryId)
        continue:'$IsObject(objCategory)
        s isDeleted=objCategory.IsDelete
        continue:isDeleted="true"
        s desc=objCategory.Name
        continue:desc=""
        s root=##class(%ArrayOfDataTypes).%New()
        d root.SetAt(categoryId,"id")
        d root.SetAt(desc,"text")
        d root.SetAt("closed","state")
        d ##class(NurMp.KnowledgeDataBase).getNode(.root,rootIndentity)
        d tree.Insert(root)
    }*/
    s tree=..getKnowledgeTreePC(locId,hospitalId,"1","","null")
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt(tree,"knowledgeTree")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetKnowledgeTree
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

ClassMethod getKnowledgeLocsPC(HospitalID As %String, LocId As %String)
{
    n (HospitalID,LocId,%session)
    s locs=##class(%ListOfDataTypes).%New()
    //改为只取跟本科室的知识库关联的科室
    s firstId=$o(^NurMp.KnowledgeLocI("Loc"," "_LocId,""),-1)
    q:firstId="" locs
    s kcId=$lg(^NurMp.KnowledgeLocD(firstId),5)
    q:kcId="" locs
    s id=""
    f {
        s id=$o(^NurMp.KnowledgeLocI("KCIndentity"," "_$zcvt(kcId,"U"),id)) 
        q:id=""
        s locId=$lg(^NurMp.KnowledgeLocD(id),2)
        s locDesc=$p($g(^CTLOC(locId)),"^",2)
        continue:locDesc=""
        s locDesc=##class(NurMp.Service.Refer.Handle).clsTranslate("User.CTLoc","GetTranByDesc","CTLOCDesc",locDesc)
        s loc=##class(%ArrayOfDataTypes).%New()
        d loc.SetAt(locId,"id")
        d loc.SetAt(locDesc,"text")
        i (locId=LocId) d loc.SetAt("1","selected")
        d locs.Insert(loc)
    }
    q locs
}

ClassMethod getKnowledgeTreePC(LocId As %String, HospitalID As %String = "", Flag As %String = "1", Filter As %String = "", ModelID As %String = "") As %String
{
    n (%session,LocId,HospitalID,Flag,Filter,ModelID)
    i Filter'="" s Flag="0"
    s tree=##class(%ListOfDataTypes).%New()
    q:LocId="" tree
    s HospitalID=##class(NurMp.Common.Base.Hosp).getHosp("Nur_IP_KnowledgeTemplate", HospitalID)
    s expandFlag=##class(NurMp.Service.Switch.Config).getDomValue(HospitalID,LocId,"KnowCateExpandFlag","String",ModelID)
    s gids=""
    s knowLocId=""
    #; f {
    #;  s knowLocId=$o(^NurMp.KnowledgeLocI("Loc"," "_LocId,knowLocId))
    #;  q:knowLocId=""
    #;  s gid=$lg(^NurMp.KnowledgeLocD(knowLocId),5)
    #;  s gids=$s(gids="":gid,1:gids_"^"_gid)
    #; }
    s id=""
    f {
        s id=$o(^NurMp.KnowledgeCategoryI("HospitalID"," "_HospitalID,id))
        q:id=""
        s objKnow=##class(NurMp.KnowledgeCategory).%OpenId(id)
        continue:'$IsObject(objKnow)
        s isDeleted=objKnow.IsDelete
        continue:isDeleted="true"
        s inpatient=objKnow.Inpatient
        continue:inpatient="null"
        s knowLocId=$p(inpatient,"^",2)
        continue:knowLocId'=LocId
        s guid=objKnow.Indentity
        #; continue:gids'[guid
        s desc=objKnow.Name
        continue:desc=""
        s root=##class(%ArrayOfDataTypes).%New()
        d root.SetAt(id,"id")
        s rootDesc=$p(desc,"^",1)
        s rootDesc=##class(NurMp.Common.Base.Lang).clsTranslate("User.CTLoc","GetTranByDesc","CTLOCDesc",rootDesc)
        s kn=$p(desc,"^",2)
        s logonLoc = $s($d(%session):%session.Data("LOGON.CTLOCID"),1:kn)
        s linkLocId=$o(^NurMp.KnowledgeLocI("LocKC"," "_logonLoc," "_$zcvt(guid,"U"),""))
        i linkLocId'="" s aliasName=$lg(^NurMp.KnowledgeLocD(linkLocId),7)
        i (Flag="1")&&($g(aliasName)'="") {
            s rootDesc=##class(NurMp.Common.Base.Lang).cspTranslate("nur.hisui.nurseKnowledgeConfig.csp",aliasName)
        }
        d root.SetAt(rootDesc,"text")
        d root.SetAt(desc,"desc")
        s locs=##class(NurMp.Service.Refer.Handle).GetListLoc(objKnow.Indentity)
        d root.SetAt(locs,"locs")
        d root.SetAt(guid,"guid")
        d root.SetAt("icon-book-green","iconCls")
        d ##class(NurMp.Service.Refer.Handle).getNode(.root,guid,HospitalID,"",Flag,Filter,expandFlag)
        i Flag="1" {
            s state="closed"
            i (expandFlag="O") || (expandFlag="P1") || ((root.GetAt("children")="")||(root.GetAt("children").Count()=0)){
                s state="open"
            }
        }else{
            s state="open"
            continue:(Filter'="")&&((root.GetAt("children")="")||(root.GetAt("children").Count()=0))
        }
        d root.SetAt(state,"state")
        d tree.Insert(root)
    }
    q tree
}

/// Creator:      fengliang
/// CreatDate:    2020-09-18
/// Description:  获取护理病历知识库
/// w ##class(Nur.MNISV3.Service.NurEmr).getKnowledgeContent(231)
ClassMethod GetKnowledgeContent(knowledgeId, userId = "", wardId = "") As %String
{
    n (knowledgeId,userId,wardId)
    s ^GRS("GetKnowledgeContent",userId_"-",knowledgeId_"-")=$lb(knowledgeId,userId,wardId)
    s $zt="errGetKnowledgeContent"
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    /*
    s objCategory=##class(NurMp.KnowledgeCategory).%OpenId(knowledgeId)
    s subKey=$zcvt(objCategory.Indentity,"U")
    s templateId=$o(^NurMp.KnowledgeTemplateI("KCIndentity"," "_subKey,""))
    i templateId'="" {
        s objTemplate=##class(NurMp.KnowledgeTemplate).%OpenId(templateId)
        s content=objTemplate.Content
        s tempIndentity=objTemplate.Indentity
        s status=##class(%XML.TextReader).ParseStream(content,.textReader)
        i ($$$ISERR(status)) s textReader=""
    }else{
        s textReader=""
    }
    s content=##class(NurMp.KnowledgeDataBase).analysisXmlByHosp(textReader)
    */
    //s content=##class(NurMp.Service.Refer.Handle).getContent(knowledgeId,"")
    
    
    s objCategory=##class(NurMp.KnowledgeCategory).%OpenId(knowledgeId)
    s subKey=$zcvt(objCategory.Indentity,"U")
    s templateId=$o(^NurMp.KnowledgeTemplateI("KCIndentity"," "_subKey,""))
    i templateId'="" {
        s objTemplate=##class(NurMp.KnowledgeTemplate).%OpenId(templateId)
        s content=objTemplate.Content
        s tempIndentity=objTemplate.Indentity
        s status=##class(%XML.TextReader).ParseStream(content,.textReader)
        i ($$$ISERR(status)) s textReader=""
    }else{
        s textReader=""
    }
    s content=##class(NurMp.Service.Refer.Handle).analysisXml(textReader,"")
    d content.SetAt(objCategory.Indentity,"Guid")
    
    
    //i '$isobject(content) s content=##class(Nur.MNISV3.Common.JSON).Decode(content)
    s contentList=##class(%ListOfDataTypes).%New()

    //i wardId=68 {
        s contentListSub=##class(%ListOfDataTypes).%New()
        s key="",titleStr=""
        f {
                s contentData=##class(%ArrayOfDataTypes).%New()
                d content.GetNext(.key)
                q:key=""
                s contentData=content.GetAt(key)
                continue:'$IsObject(contentData)
                w key_"-"_##class(Nur.JSON).Encode(contentData),!
                s contentTitle=contentData.GetAt("title")
                i contentData.GetAt("id")["Text" {
                    i titleStr'[contentTitle s titleStr=titleStr_" "_contentTitle
                }else{
                     i titleStr'[contentTitle s titleStr=titleStr_"  "_contentTitle_": ________  "
                }
                d contentListSub.Insert(contentData) 
        }
        //PDA 取的第0个
        w titleStr,!
        s obj1=content.GetAt(1)
        i $isobject(obj1) d obj1.SetAt(titleStr,"title")
        d contentList.Insert(content)
        
    //}else {
       // d contentList.Insert(content)
    //}
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"Status")
    d json.SetAt(contentList,"knowledgeContent")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)

errGetKnowledgeContent
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

ClassMethod GetKnowledgeContent2(knowledgeId) As %String
{
    n (knowledgeId)
    s $zt="errGetKnowledgeContent2"
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    
    s objCategory=##class(NurMp.KnowledgeCategory).%OpenId(knowledgeId)
    s subKey=$zcvt(objCategory.Indentity,"U")
    s templateId=$o(^NurMp.KnowledgeTemplateI("KCIndentity"," "_subKey,""))
    i templateId'="" {
        s objTemplate=##class(NurMp.KnowledgeTemplate).%OpenId(templateId)
        s content=objTemplate.Content
        s tempIndentity=objTemplate.Indentity
        s status=##class(%XML.TextReader).ParseStream(content,.textReader)
        i ($$$ISERR(status)) s textReader=""
    }else{
        s textReader=""
    }
    //s content=##class(NurMp.KnowledgeDataBase).analysisXmlByHosp(textReader)
    s content=##class(NurMp.KnowledgeDataBase).analysisXml(textReader)
    s contentList=##class(%ListOfDataTypes).%New()
    s key=""
    f {
            s contentData=##class(%ArrayOfDataTypes).%New()
            d content.GetNext(.key)
            q:key=""
            s contentData=content.GetAt(key)
            continue:'$IsObject(contentData)
            d contentList.Insert(contentData)
    }
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"Status")
    d json.SetAt(contentList,"knowledgeContent")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)

errGetKnowledgeContent2
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// Creator:      fengliang
/// CreatDate:    2020-09-18
/// Description:  获取外部数据源
/// w ##class(Nur.MNISV3.Service.NurEmr).GetOutSideData("DHCNURTemRecData","41","2022-04-14","2022-04-21")
ClassMethod GetOutSideData(outCode, episodeId, stDate, endDate) As %String
{
    n (outCode,episodeId,stDate,endDate)
    s $zt="errGetOutSideData"
    ;s ^MML("GetKnowledgeContent")=$lb(outCode,episodeId,stDate,endDate)
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    
    //抬头
    s titleInfo=""
    s fieldList=##class(%ListOfDataTypes).%New()
    s fields=##class(%ResultSet).%New("Nur.DHCNurEmrInfoReturn:getFields")
    d fields.Execute(39)
    while fields.Next()
    {
          //fieldName:fieldDesc:fieldValue:fieldType:fieldLinkNo:fieldId:
          s fieldName=fields.GetDataByName("fieldName")
          s fieldDesc=fields.GetDataByName("fieldDesc")
          s fieldValue=fields.GetDataByName("fieldValue")
          s fieldType=fields.GetDataByName("fieldType")
          s fieldLinkNo=fields.GetDataByName("fieldLinkNo")
          s fieldId=fields.GetDataByName("fieldId")
          s fieldData=##class(%ArrayOfDataTypes).%New()
          d fieldData.SetAt(fieldDesc,"fieldDesc")
          d fieldData.SetAt(fieldValue,"fieldValue")
          d fieldData.SetAt(fieldLinkNo,"fieldLinkNo")  //DoctorAdviceRuleLinkNo
          d fieldList.Insert(fieldData)
          s info="!"_fieldLinkNo_"|"_fieldValue
          s titleInfo=$s(titleInfo="":info,1:titleInfo_","_info)
    }   
    //体征列表
    s temps=##class(%ResultSet).%New("NurMp.TableDataSources:GetPatTemData")
    d temps.Execute(episodeId_"^!="_stDate_"^!="_endDate,titleInfo)
    while temps.Next()
    {
          s tempList=temps.GetDataByName("aa")
          s tempList=##class(Nur.MNISV3.Common.JSON).Decode(tempList)
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"Status")
    d json.SetAt(fieldList,"fieldList")
    d json.SetAt($g(tempList),"tempList")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)

errGetOutSideData
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// Creator：      EH
/// CreatDate：    2022-05-14
/// Description:   
/// Table:         
/// Input:         
/// Return:        
/// Other:         
ClassMethod ZObjProperty(rec, item) As %String
{
    n (rec,item)
    try { s value=$zobjproperty(rec,item) } catch { s value="" }
    q value
}

ClassMethod IsFilled(episodeId, emrCode) As %String
{
    n (episodeId,emrCode)
    s ret=0
    s TCIdentity=##class(NurMp.Template).GetTCIndentity(emrCode)
    s rw="" f  s rw=$o(^NurMp.DHCTempMultDataI("EmrCode"," "_$zcvt(TCIdentity,"u")," "_episodeId,rw)) q:(rw="")||(ret=1)  d
    .s data=$g(^NurMp.DHCTempMultDataD(rw))
    .q:($lg(data,73)'="")
    .s ret=1
    q ret
}

ClassMethod NumberValue(value) As %Stream
{
    n (value)
    i ($e(value)="[")&&($e(value,*)="]") d
    .s list=##class(Nur.MNISV3.Common.JSON).Decode(value)
    .i ($isobject(list)) d
    ..s obj=list.GetAt(1)
    ..s value=obj.GetAt("NumberValue")
    e  i ($e(value,1,2)="CA")&&($l(value,"*")=2) d
    .s value=$e($p(value,"*",1),3,$l($p(value,"*",1)))
    q value
}

}
